#encoding:utf-8
'''
Created on 2012/2/2
此為從attribute + class的檔案中取出sample的程式
並且將取出的class輸出到參數三
@author: Netdb_Heng
'''
#參數一為母體，參數二為輸入Label，參數三為輸出sample，參數四為輸出sample的class，參數五為筆數

import sys,random

print ( sys.argv[1] + " to " + sys.argv[2] + ":" + sys.argv[3])

inf = open(sys.argv[1])
infattrs = open(sys.argv[2])
infclass = open(sys.argv[3])

outf = open(sys.argv[4],'w')
outattrs = open(sys.argv[5],'w')
outclass = open(sys.argv[6],'w')

sample_num = int(sys.argv[7])
choose_record = []      #挑選過的

lines = inf.readlines()
class_lines = infclass.readlines()
attrs_lines = infattrs.readlines()
outf.write(lines[0])
while len(choose_record)<sample_num:
    choose=0
    random_int = random.randint(1,len(lines)-1)     #隨機1~len(lines)-1
    temp = lines[random_int].split()
    tempClass = class_lines[random_int-1].split()
    tempAttrs = attrs_lines[random_int-1].split()
    for j in range(len(choose_record)):
        if random_int == choose_record[j]:
            choose=1
            break
    if choose==0:
        choose_record.append(random_int)
        outf.write(temp[0] + '\n')
        outclass.write(tempClass[0] + '\n')
        outattrs.write(tempAttrs[0] + '\n')        
inf.close();
outf.close();

